Skip to main content

Applying Functions

df.get(column_name).apply(function_name)

Applies a function of one parameter to every entry in the column.

Input:
function_name: a python function
The function to apply to every entry in the column. This function should take a single parameter and return a value.
Returns:
A Series of the same size containing the results of the function application.
Return Type:
Series:
The returned Series will have the same index as the input Series and will contain the transformed values based on the applied function.

pets
IndexSpeciesColorWeightAge
0dogblack405
1catgolden158
2catblack209
3dogwhite802
4dogblack250.5
5hamsterblack13
6hamstergolden0.250.2
pets.get('Species').apply(is_dog)
  • 0"True"
  • 1"False"
  • 2"False"
  • 3"True"
  • 4"True"
  • 5"False"
  • 6"False"

pets.get('Weight').apply(np.sqrt)
  • 0"6.324555"
  • 1"3.872983"
  • 2"4.472136"
  • 3"8.944272"
  • 4"5.000000"
  • 5"1.000000"
  • 6"0.500000"

(Refer back to Writing Functions for categorize_animal.)

pets.get('ID').apply(categorize_animal)
  • 0"Adult Normal"
  • 1"Kitten Underweight"
  • 2"Adult Overweight"
  • 3"Adult Overweight"
  • 4"Puppy Normal"
  • 5"Senior Overweight"
  • 6"Young Normal"
  • 7"Kitten Normal"